package jsqlite;

import java.io.PrintStream;
import java.io.PrintWriter;

/* loaded from: classes2.dex */
public class SQLDump implements Callback {
    Database db;
    PrintWriter err;
    PrintWriter pw;
    Shell s;

    @Override // jsqlite.Callback
    public void columns(String[] strArr) {
    }

    @Override // jsqlite.Callback
    public void types(String[] strArr) {
    }

    public SQLDump(PrintWriter printWriter, Database database) {
        this.pw = printWriter;
        this.err = printWriter;
        this.db = database;
        Shell shell = new Shell(printWriter, printWriter);
        this.s = shell;
        shell.mode = 5;
        this.s.db = database;
    }

    public SQLDump(PrintStream printStream, Database database) {
        PrintWriter printWriter = new PrintWriter(printStream);
        this.pw = printWriter;
        this.err = printWriter;
        this.db = database;
        Shell shell = new Shell(printWriter, printWriter);
        this.s = shell;
        shell.mode = 5;
        this.s.db = database;
    }

    public void dump() throws Exception {
        this.pw.println("BEGIN TRANSACTION;");
        this.db.exec("SELECT name, type, sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL ORDER BY substr(type,2,1), name", this);
        this.pw.println("COMMIT;");
        this.pw.flush();
    }

    @Override // jsqlite.Callback
    public boolean newrow(String[] strArr) {
        if (strArr.length != 3) {
            return true;
        }
        PrintWriter printWriter = this.pw;
        printWriter.println(String.valueOf(strArr[2]) + ";");
        if (strArr[1].compareTo("table") == 0) {
            this.s.mode = 5;
            this.s.set_table_name(strArr[0]);
            String[] strArr2 = {strArr[0]};
            try {
                String str = "SELECT * from '%q'";
                if (this.s.db.is3()) {
                    TableResult tableResult = this.s.db.get_table("PRAGMA table_info('%q')", strArr2);
                    if (tableResult != null) {
                        StringBuffer stringBuffer = new StringBuffer();
                        String str2 = "";
                        stringBuffer.append("SELECT ");
                        for (int i = 0; i < tableResult.nrows; i++) {
                            String str3 = ((String[]) tableResult.rows.elementAt(i))[1];
                            stringBuffer.append(str2 + "quote(" + Shell.sql_quote_dbl(str3) + ")");
                            str2 = ",";
                        }
                        stringBuffer.append(" from '%q'");
                        str = stringBuffer.toString();
                        this.s.mode = 6;
                    }
                    this.s.db.exec(str, this.s, strArr2);
                    this.pw.flush();
                } else {
                    this.s.db.exec("SELECT * from '%q'", this.s, strArr2);
                    this.pw.flush();
                }
            } catch (Exception unused) {
                return true;
            }
        }
        this.s.mode = 0;
        return false;
    }
}
